<div id="html-template" class="hide">
    <li class="commodity-require-comment">
        <div class="comment-avatar-block"><img class="avatar" src="" alt="用户头像" style="max-width:100%; max-height:100%"/></div>
        <div class="comment-main-block">
            <div class="username">#username#</div>
            <div class="time">#time#</div>
            <div><span class="private"></span><span class="father-comment-username"></span><span class="comment-content">#content#</span></div>
            <a href="javascript:" class="leave-comment-reply-btn">回复</a>
        </div>
        <form class="leave-commodity-require-comment" method="post" style="display: none">
            <div class="form-content" style="overflow: hidden">
                <input type="hidden" name="commodity_require_id">
                <input type="hidden" name="father_id">

                <div class="input-group">
                    <label class="input-group-addon sr-only">回复</label>
                    <textarea name="content" class="form-control reply-textarea" placeholder="请输入回复内容" required="required"></textarea>
                </div>
                <div class="pull-right">
                    <input type="checkbox" name="private" style="vertical-align:-2px"/>
                    <span class="secret-txt">密谈</span>
                    <input type="submit" class="btn btn-primary" value="发表"/>
                </div>
            </div>
        </form>
        <ul class="sub-comment-block"></ul>
    </li>
    <div class="load-more-comment-block">加载更多……</div>
</div>

<?php /** @var User $CurrentUser */ ?>
<input type="hidden" value="<?php echo ($CurrentUser->getPower() < 1) ? '0' : '1'; ?>" id="ifpower"/>
<?php if (isset($CommodityRequireArray) && sizeof($CommodityRequireArray) > 0) : ?>
    <div class="left-right-side">
    <div class="left-side">
        <?php foreach ($CommodityRequireArray as $index => $thisCommodityRequire): ?>
            <?php /** @var CommodityRequire $thisCommodityRequire */ ?>
            <?php if (!$thisCommodityRequire) continue; ?>
            <?php if ($index % 2 == 1) continue; ?>
            <?php /** @var User $thatUser */ ?>
            <?php if (!$thatUser = $thisCommodityRequire->getUser()) continue; ?>
            <div id="commodity-require-<?php echo $thisCommodityRequire->getId() ?>" class="commodity-require" data-commodity-require-id="<?php echo $thisCommodityRequire->getId() ?>">
                <div>
                    <div class="user-info">
                        <a href="<?php echo base_url('home/userCenter/commodity/' . $thatUser->getId()) ?>">
                            <img src="<?php echo $thatUser->getDisplayAvatarUrl() ?>" alt="头像"/>

                            <p><?php echo $thatUser->getDisplayName(); ?></p>
                        </a>
                            <span class="form-here2">
                                <?php
                                if ($currentSchool['schoolid'] == 0) {
                                    //注意:可能会有性能问题--kwan
                                    $_commodity_school_unicode = $thisCommodityRequire->getSchoolunicode();
                                    $_commodity_school = School_Model::getSchoolByUnicode($_commodity_school_unicode);
                                    if ($_commodity_school) {
                                        echo '来自' . $_commodity_school->getName();
                                    }
                                }
                                ?>
                            </span>
                    </div>
                    <div class="clearfix"></div>
                    <div class="commodity-require-name">
                        <span class="text-primary" href="#"><?php echo $thisCommodityRequire->getTitle(); ?></span>
                    </div>

                    <div class="commodity-require-content">
                        <?php echo $thisCommodityRequire->getContent(); ?>
                    </div>

                    <ul class="nav nav-pills nav-justified nowrap" style="padding-left:30px; margin-bottom:10px;">
                        <li class="price"><?php echo $thisCommodityRequire->getMinPrice(); ?> ~ <?php echo $thisCommodityRequire->getMaxPrice(); ?></li>
                        <li>
                            <?php if (isset($CurrentUser) && $CurrentUser->getId() !== 0): ?>
                                <?php switch ($thisCommodityRequire->getContactType()) :
                                    case _ContactType_LongPhone:
                                    case _ContactType_ShortPhone:
                                        ?>
                                        <div class="contact_phone"><?php echo $thisCommodityRequire->getDisplayContactInfo(); ?></div>
                                        <?php
                                        break;
                                    case _ContactType_Qq:
                                        ?>
                                        <a class="contact_qq" style="padding:0; text-align:left; padding-left:28px; margin-left:-8px;" target="_blank"
                                           href="http://wpa.qq.com/msgrd?v=3&uin=<?php echo $thisCommodityRequire->getDisplayContactInfo(); ?>&site=qq&menu=yes"><?php echo $thisCommodityRequire->getDisplayContactInfo(); ?></a>
                                        <?php
                                        break;
                                    case _ContactType_None:
                                    default :
                                        ?>
                                            <div class="contact_no"><?php echo $thisCommodityRequire->getDisplayContactInfo(); ?></div>
                                        <?php
                                endswitch;
                                ?>
                            <?php else: ?>
                                <div class="contact_phone"><a class="signIn" href="javascript:" style="text-decoration:underline; line-height:22px;">登录后查看</a></div>
                            <?php endif; ?>
                        </li>
                    </ul>
                </div>

                <div class="meta">
                    <div class="pull-left">
                        <div class="time"><?php echo $thisCommodityRequire->getReleaseTime(); ?></div>
                    </div>
                    <div class="pull-right" style="position:relative;">
                        <div class="share-box">
                            <div class="share-to">
                                <a href="<?php echo base_url('api/share?type=weibo&commodityrequire_id='.$thisCommodityRequire->getId()) ?>"><img src="<?php echo base_url('public/images/icon/details/weibo.png') ?>" title="分享到微博"/></a>
                            </div>
                            <a href="javascript:"><span class="glyphicon glyphicon-share"></span>分享</a><span class="top_line">|</span>
                        </div>
                        <a href="javascript:" class="show-comment" data-commodity-require-id="<?php echo $thisCommodityRequire->getId() ?>"><span class="glyphicon glyphicon-comment"></span>留言(<?php echo $thisCommodityRequire->getCommodityRequireCommentCount() ?>)</a>
                    </div>
                </div>
                <div class="comment">
                    <div class="leave-comment">
                        <?php if (isset($CurrentUser) && $CurrentUser->getId() !== 0): ?>
                            <form class="leave-commodity-require-comment" method="post" data-master-comment-id="0">
                                <div class="form-content" style="overflow: hidden">
                                    <input type="hidden" name="commodity_require_id" value="<?php echo $thisCommodityRequire->getId() ?>">
                                    <input type="hidden" name="father_id" value="0">

                                    <div class="input-group">
                                        <label class="input-group-addon sr-only">回复</label>
                                        <textarea name="content" class="form-control reply-textarea" placeholder="请输入回复内容" required="required"></textarea>
                                    </div>
                                    <div class="pull-right">
                                        <input type="checkbox" name="private" style="vertical-align:-2px"/>
                                        <span class="secret-txt">密谈</span>
                                        <input type="submit" class="btn btn-primary" value="发表"/>
                                    </div>
                                </div>
                            </form>
                        <?php else: ?>
                            <div class="comment-user-nologin">
                                要留言？请先&nbsp;&nbsp;<a class="signIn" href="javascript:">登录</a>&nbsp;&nbsp;或&nbsp;&nbsp;<a class="signUp" href="javascript:">注册</a>
                            </div>
                        <?php endif; ?>
                    </div>
                    <ul class="list-comment list-unstyled"></ul>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    <div class="right-side">
        <?php foreach ($CommodityRequireArray as $index => $thisCommodityRequire): ?>
            <?php /** @var CommodityRequire $thisCommodityRequire */ ?>
            <?php if (!$thisCommodityRequire) continue; ?>
            <?php if ($index % 2 == 0) continue; ?>
            <?php /** @var User $thatUser */ ?>
            <?php if (!$thatUser = $thisCommodityRequire->getUser()) continue; ?>
            <div id="commodity-require-<?php echo $thisCommodityRequire->getId() ?>" class="commodity-require">
                <div>
                    <div class="user-info">
                        <a href="<?php echo base_url('home/userCenter/commodity/' . $thatUser->getId()) ?>">
                            <img src="<?php echo $thatUser->getDisplayAvatarUrl() ?>" alt="头像"/>

                            <p><?php echo $thatUser->getDisplayName(); ?></p>
                        </a>
                            <span class="form-here2"><?php
                                if ($currentSchool['schoolid'] == 0) {
                                    //注意:可能会有性能问题--kwan
                                    $_commodity_school_unicode = $thisCommodityRequire->getSchoolunicode();
                                    $_commodity_school = School_Model::getSchoolByUnicode($_commodity_school_unicode);
                                    if ($_commodity_school) {
                                        echo '来自' . $_commodity_school->getName();
                                    }
                                }
                                ?>
                            </span>
                    </div>
                    <div class="clearfix"></div>
                    <div class="commodity-require-name">
                        <span class="text-primary" href="#"><?php echo $thisCommodityRequire->getTitle(); ?></span>
                    </div>

                    <div class="commodity-require-content">
                        <?php echo $thisCommodityRequire->getContent(); ?>
                    </div>

                    <ul class="nav nav-pills nav-justified nowrap" style="padding-left:30px; margin-bottom:10px;">
                        <li class="price"><?php echo $thisCommodityRequire->getMinPrice(); ?> ~ <?php echo $thisCommodityRequire->getMaxPrice(); ?></li>
                        <li>
                            <?php if (isset($CurrentUser) && $CurrentUser->getId() !== 0): ?>
                                <?php switch ($thisCommodityRequire->getContactType()) :
                                    case _ContactType_LongPhone:
                                    case _ContactType_ShortPhone:
                                        ?>
                                        <div class="contact_phone"><?php echo $thisCommodityRequire->getDisplayContactInfo(); ?></div>
                                        <?php
                                        break;
                                    case _ContactType_Qq:
                                        ?>
                                        <a class="contact_qq" style="padding:0; text-align:left; padding-left:28px; margin-left:-8px;" target="_blank"
                                           href="http://wpa.qq.com/msgrd?v=3&uin=<?php echo $thisCommodityRequire->getDisplayContactInfo(); ?>&site=qq&menu=yes"><?php echo $thisCommodityRequire->getDisplayContactInfo(); ?></a>
                                        <?php
                                        break;
                                    case _ContactType_None:
                                    default :
                                        ?>
                                            <div class="contact_no"><?php echo $thisCommodityRequire->getDisplayContactInfo(); ?></div>
                                        <?php
                                endswitch;
                                ?>
                            <?php else: ?>
                                <div class="contact_phone"><a class="signIn" href="javascript:" style="text-decoration:underline; line-height:22px;">登录后查看</a></div>
                            <?php endif; ?>
                        </li>
                    </ul>
                </div>

                <div class="meta">
                    <div class="pull-left">
                        <div class="time"><?php echo $thisCommodityRequire->getReleaseTime(); ?></div>
                    </div>
                    <div class="pull-right" style="position:relative;">
                        <div class="share-box">
                            <div class="share-to">
                                <a href="<?php echo base_url('api/share?type=weibo&commodityrequire_id='.$thisCommodityRequire->getId()) ?>"><img src="<?php echo base_url('public/images/icon/details/weibo.png') ?>" title="分享到微博"/></a>
                            </div>
                            <a href="javascript:"><span class="glyphicon glyphicon-share"></span>分享</a><span class="top_line">|</span>
                        </div>
                        <a href="javascript:" class="show-comment" data-commodity-require-id="<?php echo $thisCommodityRequire->getId() ?>"><span class="glyphicon glyphicon-comment"></span>留言(<span
                                class="comment-count"><?php echo $thisCommodityRequire->getCommodityRequireCommentCount() ?></span>)</a>
                    </div>
                </div>

                <div class="comment">
                    <div class="leave-comment">
                        <?php if (isset($CurrentUser) && $CurrentUser->getId() !== 0): ?>
                            <form class="leave-commodity-require-comment" method="post" data-master-comment-id="0">
                                <div class="form-content" style="overflow: hidden">
                                    <input type="hidden" name="commodity_require_id" value="<?php echo $thisCommodityRequire->getId() ?>">
                                    <input type="hidden" name="father_id" value="0">

                                    <div class="input-group">
                                        <label class="input-group-addon sr-only">回复</label>
                                        <textarea name="content" class="form-control reply-textarea" placeholder="请输入回复内容" required="required"></textarea>
                                    </div>
                                    <div class="pull-right">
                                        <input type="checkbox" name="private" style="vertical-align:-2px"/>
                                        <span class="secret-txt">密谈</span>
                                        <input type="submit" class="btn btn-primary" value="发表"/>
                                    </div>
                                </div>
                            </form>
                        <?php else: ?>
                            <div class="comment-user-nologin">
                                要留言？请先&nbsp;&nbsp;<a class="signIn" href="javascript:">登录</a>&nbsp;&nbsp;或&nbsp;&nbsp;<a class="signUp" href="javascript:">注册</a>
                            </div>
                        <?php endif; ?>
                    </div>
                    <ul class="list-comment list-unstyled"></ul>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    </div>
<?php else : ?>
    <div>
        <h1 class="alert alert-success text-center">Sorry，暂时没有您要的信息！<a class="changeschool" href="###">去其他学校看看</a></h1>
    </div>
<?php endif ?>

<script>
    function appendCommodityRequireComment($block, comment, fatherUsername, masterCommentId) {
        var $comment = $('#html-template').find('.commodity-require-comment').clone().addClass('hide').appendTo($block);
        $comment.attr('id', 'commodity-require-comment-' + comment.id);
        $comment.attr('data-comment-id', comment.id);
        $comment.find('.avatar').attr('src', comment.userAvatarUrl);
        $comment.find('.username').html(comment.userDisplayName);
        $comment.find('.time').html(comment.releaseTime);
        $comment.find('.comment-content').html(comment.content);
        $comment.find('form.leave-commodity-require-comment-reply').attr('data-ajax-append-id', masterCommentId);
        var $leaveCommentForm = $comment.find('.leave-commodity-require-comment');
        $leaveCommentForm.attr('data-father-name', comment.userDisplayName);
        $leaveCommentForm.attr('data-master-comment-id', masterCommentId);
        $leaveCommentForm.find('input[name=commodity_require_id]').val(comment.commodityRequireId);
        $leaveCommentForm.find('input[name=father_id]').val(comment.id);
        if (comment.private == true)
            $comment.find('.private').html('[密谈]');
        if (fatherUsername)
            $comment.find('.father-comment-username').html('@' + fatherUsername);
        return $comment;
    }


    function appendCommodityRequireCommentByCommentSet(commentSet) {
        var perPage = 4;
        var i = 0;
        var commodityRequireId = commentSet.commodityRequireId;
        var $commentList = $('#commodity-require-' + commodityRequireId).find('.list-comment');
        commentSet.masterCommentIdArraySortByReleaseTime.forEach(function (commentId) {
            var comment = commentSet.comments[commentId];
            if (comment) {
                var j = 0;
                var $comment = appendCommodityRequireComment($commentList, comment, undefined, comment.id);
                var $subCommentBlock = $comment.find('.sub-comment-block');
                comment.descendantCommentIdArraySortByReleaseTime.forEach(function (subCommentId) {
                    var subComment = commentSet.comments[subCommentId];
                    var $sub = appendCommodityRequireComment($subCommentBlock, subComment, commentSet.comments[subComment.fatherId] ? commentSet.comments[subComment.fatherId].userDisplayName : undefined, comment.id);
                    if (j < perPage) {
                        $sub.removeClass('hide');
                    }
                    if (j == perPage) {
                        appendLoadMoreCommodityCommentBlock($subCommentBlock);
                    }
                    j++;
                });
                if (i < perPage) {
                    $comment.removeClass('hide');
                }
                if (i == perPage) {
                    appendLoadMoreCommodityCommentBlock($commentList);
                }
                i++;
            }
        });
    }

    function appendCommodityRequireCommentByCommodityRequireId(commodity_id) {
        CommodityRequireCommentSet.InitByCommodityRequireId(commodity_id, appendCommodityRequireCommentByCommentSet);
    }

    function appendLoadMoreCommodityCommentBlock($block) {
        var $loadMoreBtn = $('#html-template').find('.load-more-comment-block').clone().addClass('hide').appendTo($block);
        $loadMoreBtn.removeClass('hide');
        return $loadMoreBtn;
    }

    function hideAllLeaveCommentForm() {
        $('form.leave-commodity-require-comment[data-master-comment-id!=0]').slideUp();
    }

    $(document).on('click', '.load-more-comment-block', function () {
        var perClickLoadNum = 5;
        $(this).nextAll('.commodity-require-comment.hide').each(function (k, v) {
            if (k < perClickLoadNum) {
                $(v).removeClass('hide');
            }
            if (k == perClickLoadNum) {
                appendLoadMoreCommodityCommentBlock($(this).parent());
            }
        });
        $(this).remove();
    });

    $(document).on('submit', 'form.leave-commodity-require-comment', function (event) {
        event.preventDefault();
        var ifpower = $("#ifpower").val();
        if (ifpower == 0) {
            noty({
                type: 'warning',
                text: '请先激活邮箱帐号，并进行实名认证，Lets Go？',
                buttons: [
                    {
                        addClass: 'btn btn-sm btn-danger', text: '确定',
                        onClick: function ($noty) {
                            $noty.close();
                            window.location.href = base_url('home/userCenter#resendemail');
                        }
                    },
                    {
                        addClass: 'btn btn-sm btn-default', text: '取消',
                        onClick: function ($noty) {
                            $noty.close();
                        }
                    }
                ]
            });
        }
        else {
            var form = this;
            var fatherName = $(form).data('father-name');
            var masterCommentId = $(form).data('master-comment-id');
            $.ajax({
                url: base_url('api/leaveCommodityRequireComment'),
                type: 'post',
                data: $(form).serialize(),
                success: function (jsonrsp) {
                    try {
                        var rsp = $.parseJSON(jsonrsp);
                        switch (rsp.type) {
                            case 'success':
                                noty({type: rsp.type, text: rsp.message});
                                var $commentCount = $(form).closest('.commodity-require').find('.comment-count');
                                var commentCount = $commentCount.text();
                                $commentCount.text(+commentCount + +1);
                                $(form).find('[name=content]').val('');
                                var comment = CommodityRequireComment.InitByJsonData(rsp.data);
                                hideAllLeaveCommentForm();
                                var $comment = undefined;
                                if (masterCommentId != 0)
                                    $comment = appendCommodityRequireComment($('#commodity-require-comment-' + masterCommentId).children('.sub-comment-block'), comment, fatherName, masterCommentId);
                                else
                                    $comment = appendCommodityRequireComment($('#commodity-require-' + comment.commodityRequireId).find('.list-comment'), comment, fatherName, comment.id);
                                $comment.removeClass('hide');
                                break;
                            default:
                                noty({type: rsp.type, text: rsp.message});
                                break;
                        }
                    } catch (ex) {
                        noty({type: 'error', text: '发生内部错误，请联系管理员'});
                        throw ex;
                    }
                }
            });
        }
    });

    $(document).on('click', '.show-comment', function () {
        var commodityRequireId = $(this).data('commodity-require-id');
        var $thisCommodity = $('#commodity-require-' + commodityRequireId);
        var $comment = $thisCommodity.find('.comment');
        var $commentList = $comment.find('.list-comment');
        if ($comment.css('display') == 'none') {
            $commentList.empty();
            $comment.slideDown('fast');
            $(this).css('color', 'rgb(255, 250, 148)');
            appendCommodityRequireCommentByCommodityRequireId(commodityRequireId);
        } else {
            $comment.slideUp('fast');
            $(this).removeAttr('style');
        }
    });


    $(document).on('click', '.leave-comment-reply-btn', function () {
        var commodityRequireId = $(this).closest('.commodity-require').data('commodity-require-id');
        var commentId = $(this).closest('.commodity-require-comment').data('comment-id');
        var $comment = $(this).closest('li#commodity-require-comment-' + commentId);
        var $leaveComment = $comment.children('.leave-commodity-require-comment');
        if ($leaveComment.css('display') == 'none') {
            hideAllLeaveCommentForm();
            $leaveComment.slideDown();
        } else {
            $leaveComment.slideUp();
        }
    });
</script>
<script type="text/javascript">
    $(document).on('mouseenter', '.share-box', function () {
        $(this).children(".share-to").css("display", "block");
        $(".share-to").not(this).children(".share-to").css("display", "none");
    })
    $(document).on('mouseleave', '.share-box', function () {
        $(this).children(".share-to").css("display", "none");
    })
</script>